package com.xieyong.mywebsite.controller;

import com.github.pagehelper.PageInfo;
import com.xieyong.mywebsite.model.dto.Result;
import com.xieyong.mywebsite.model.entity.User;
import com.xieyong.mywebsite.service.UserService;
import com.xieyong.mywebsite.util.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.Map;


/**
 * 管理员管理页面的接口
 * */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    UserService userService;

    @Autowired
    private UserController userController;

    /**
     *
     * @param pageNum  当前页码
     * @param pageSize  每页个数
     * @return
     */

    @GetMapping("/page")
    public Result<PageInfo<User>> selectPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            User user) {
        user.setRole("ADMIN");
        PageInfo<User> userPageInfo = userService.getPage(pageNum, pageSize,user);
        return Result.success(userPageInfo);
    }

    @PostMapping("/export")
    public void exportData(HttpServletResponse response, @RequestBody(required = false) List<User> userList) throws Exception {
        if (userList == null || userList.isEmpty()) {
            userService.exportAll(response, "ADMIN");
        } else {
            userService.exportBatch(response, userList, "ADMIN");
        }
    }

    @PostMapping("/import")
    public Result<File> importData(MultipartFile file) throws Exception {
        return userService.importData(file);
    }

    @GetMapping()
    public Result<List<User>> getAll() {
        List<User> admin = userService.getAllByRole("ADMIN");
        return Result.success(admin);
    }

/**
 *   下面的接口直接调用usercontroller的方法
 *
 *
 * */

    @PostMapping("")
    public Result<String> add(@RequestBody @Validated User user) { // @RequestBody 接收前端传来的 json参数
        user.setRole("ADMIN");
        return userController.add(user);
    }

    @PutMapping("")
    public Result<User> update(@RequestBody @Validated User user) { // @RequestBody 接收前端传来的 json参数
        user.setRole("ADMIN");
        return userController.update(user);
    }

    @PatchMapping("/update-avatar")
    public Result<User> updateAvatar(@RequestParam @URL String avatarUrl) {
        return userController.updateAvatar(avatarUrl);
    }

    @PatchMapping("/update-password")
    public Result<User> updatePassword(@RequestBody Map<String, String> params,@RequestHeader("Authorization") String token) {
        return userController.updatePassword(params,token);
    }


    @DeleteMapping("/{id}")
    public Result<User> delete(@PathVariable Integer id) { // @PathVariable 接收前端传来的 路径参数
        return userController.delete(id);
    }

    @DeleteMapping("/delete-batch")
    public Result<User> deleteBatch(@RequestBody List<Integer> ids) { //  @RequestBody 接收前端传来的 json参数
        return userController.deleteBatch(ids);
    }

    @GetMapping("/user-info")
    public Result<User> userInfo() {
        return userController.userInfo();
    }

    @GetMapping("/{id}")
    public Result<User> getOne(@PathVariable Integer id) {
        return userController.getOne(id);
    }
}
